<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../">
  <title data-ice="title">utils/markdown.js | API</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  <script src="script/manual.js"></script>
<link rel="stylesheet" href="./inject/css/0-vue.css"><link rel="stylesheet" href="./inject/css/0-app.css"><link rel="stylesheet" href="./inject/css/0-esdoc.css"><script src="./inject/script/0-custom.js"></script><meta name="description" content="&#x4F01;&#x4E1A;&#x5373;&#x65F6;&#x901A;&#x8BAF;&#x5E73;&#x53F0;"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="API"><meta property="twitter:description" content="&#x4F01;&#x4E1A;&#x5373;&#x65F6;&#x901A;&#x8BAF;&#x5E73;&#x53F0;"></head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
<a style="position:relative; top:3px;" href="https://github.com/easysoft/xuanxuan.git"><img width="20px" src="./image/github.png"></a></header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/color.js~Color.html">Color</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/delay-action.js~DelayAction.html">DelayAction</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/status.js~Status.html">Status</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/status.js~StatusKeeper.html">StatusKeeper</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/task-queue.js~TaskQueue.html">TaskQueue</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createDate">createDate</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createPhpTimestramp">createPhpTimestramp</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatDate">formatDate</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatSpan">formatSpan</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getTimeBeforeDesc">getTimeBeforeDesc</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isSameDay">isSameDay</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isSameMonth">isSameMonth</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isSameYear">isSameYear</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isToday">isToday</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isYestoday">isYestoday</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-classes">classes</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-escape">escape</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getSearchParam">getSearchParam</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isWebUrl">isWebUrl</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-linkify">linkify</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-rem">rem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-strip">strip</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-cutImage">cutImage</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getImageInfo">getImageInfo</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-limittimepromise">limittimepromise</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-mdifileicon">mdifileicon</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-pinyin">pinyin</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-plain">plain</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-matchScore">matchScore</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatKeyDecoration">formatKeyDecoration</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getKeyDecoration">getKeyDecoration</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isOnlyModifyKeys">isOnlyModifyKeys</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getCodeFromString">getCodeFromString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-longShadow">longShadow</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-skinStyle">skinStyle</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-sortList">sortList</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-clearStore">clearStore</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getStoreItem">getStoreItem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getStoreLength">getStoreLength</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-removeStoreItem">removeStoreItem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-setStoreItem">setStoreItem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-storeForEach">storeForEach</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-storeGetAll">storeGetAll</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatBytes">formatBytes</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatString">formatString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-ifEmptyStringThen">ifEmptyStringThen</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isEmptyString">isEmptyString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isNotEmptyString">isNotEmptyString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-timesequence">timesequence</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-TIME_DAY">TIME_DAY</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-renderer">renderer</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-fileIcons">fileIcons</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-BYTE_UNITS">BYTE_UNITS</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">utils/markdown.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import Marked from &apos;marked&apos;;
import HighlightJS from &apos;highlight.js&apos;;
import HTMLParser from &apos;htmlparser&apos;;
import Config from &apos;../config&apos;;
import Lang from &apos;../lang&apos;;
import {strip} from &apos;./html-helper&apos;;

/** @module markdown */

/**
 * Marked &#x6E32;&#x67D3;&#x5B9E;&#x4F8B;
 * @constant
 * @see https://github.com/markedjs/marked
 */
export const renderer = new Marked.Renderer();

// &#x91CD;&#x6784;&#x4EE3;&#x7801;&#x5757;&#x7684;&#x6E32;&#x67D3;
renderer.code = (code, lang) =&gt; {
    let fileName = null;
    if (lang) {
        const colonIndex = lang.indexOf(&apos;:&apos;);
        const dotIndex = lang.lastIndexOf(&apos;.&apos;);
        if (colonIndex &gt; -1) {
            fileName = lang.substr(colonIndex + 1);
            lang = lang.substr(0, colonIndex);
        } else if (dotIndex &gt; -1) {
            fileName = lang;
            lang = lang.substr(dotIndex + 1);
        }
    }
    const result = HighlightJS.highlightAuto(code, lang ? [lang] : undefined);
    return `&lt;pre class=&quot;code-block&quot; ${fileName ? (` data-name=&quot;${fileName}&quot;`) : &apos;&apos;}&gt;&lt;div class=&quot;hint--left btn-copy-code app-link&quot; data-url=&quot;!copyCode/${lang || &apos;&apos;}&quot; data-hint=&quot;${Lang.string(&apos;common.copyCode&apos;)}&quot;&gt;&lt;button class=&quot;btn iconbutton rounded primary-pale text-primary&quot; type=&quot;button&quot;&gt;&lt;i class=&quot;icon mdi mdi-code-not-equal-variant icon-2x&quot;&gt;&lt;/i&gt;&lt;/button&gt;&lt;/div&gt;&lt;code data-lang=&quot;${lang || &apos;&apos;}&quot; class=&quot;lang-${result.language}&quot;&gt;${result.value}&lt;/code&gt;&lt;/pre&gt;`;
};

// &#x901A;&#x7528;&#x5C5E;&#x6027;
const commonAttrs = new Set([&apos;class&apos;]);

// Markdown &#x4E2D;&#x5141;&#x8BB8;&#x7684;&#x6807;&#x7B7E;
const allowedTags = {
    a: new Set([&apos;class&apos;, &apos;href&apos;, &apos;title&apos;]),
    b: commonAttrs,
    blockquote: commonAttrs,
    code: true,
    em: commonAttrs,
    h1: commonAttrs,
    h2: commonAttrs,
    h3: commonAttrs,
    h4: commonAttrs,
    h5: commonAttrs,
    h6: commonAttrs,
    li: commonAttrs,
    ol: commonAttrs,
    ul: commonAttrs,
    p: commonAttrs,
    pre: commonAttrs,
    address: commonAttrs,
    s: commonAttrs,
    i: commonAttrs,
    sub: commonAttrs,
    sup: commonAttrs,
    strong: commonAttrs,
    kbd: true,
    del: true,
    mark: true,
    ins: true,
    hr: true,
    var: true,
    table: commonAttrs,
    tr: commonAttrs,
    thead: commonAttrs,
    th: new Set([&apos;class&apos;, &apos;colspan&apos;, &apos;rowspan&apos;]),
    td: new Set([&apos;class&apos;, &apos;colspan&apos;, &apos;rowspan&apos;]),
    tfoot: commonAttrs,
    tbody: commonAttrs,
    img: new Set([&apos;class&apos;, &apos;src&apos;, &apos;alt&apos;]),
    video: new Set([&apos;class&apos;, &apos;controls&apos;, &apos;autoPlay&apos;, &apos;buffered&apos;, &apos;crossorigin&apos;, &apos;height&apos;, &apos;loop&apos;, &apos;muted&apos;, &apos;preload&apos;, &apos;poster&apos;, &apos;width&apos;, &apos;playsinline&apos;, &apos;src&apos;]),
    source: new Set([&apos;src&apos;, &apos;type&apos;]),
    audio: new Set([&apos;class&apos;, &apos;autoplay&apos;, &apos;buffered&apos;, &apos;controls&apos;, &apos;crossorigin&apos;, &apos;loop&apos;, &apos;muted&apos;, &apos;preload&apos;, &apos;src&apos;]),
    track: new Set([&apos;default&apos;, &apos;kind&apos;, &apos;label&apos;, &apos;src&apos;, &apos;srclang&apos;]),
    div: commonAttrs,
    span: commonAttrs,
    dl: commonAttrs,
    dt: commonAttrs,
    dd: commonAttrs,
    abbr: commonAttrs,
    details: new Set([&apos;class&apos;, &apos;open&apos;]),
    summary: commonAttrs,
    caption: commonAttrs,
};

// see https://github.com/tautologistics/node-htmlparser
const htmlParserHandler = new HTMLParser.DefaultHandler();
const sanitizer = tag =&gt; {
    const isCloseTag = tag.startsWith(&apos;&lt;/&apos;);
    if (isCloseTag) {
        const tagName = tag.substring(2, tag.length - 1);
        return allowedTags[tagName] ? tag : strip(tag);
    }
    const indexOfFirstSpace = tag.indexOf(&apos; &apos;);
    const hasAttrs = indexOfFirstSpace &gt; 0;
    const tagName = tag.substring(1, hasAttrs ? (indexOfFirstSpace) : (tag.length - 1));
    const allowedRule = allowedTags[tagName];
    if (!allowedRule) {
        return strip(tag);
    }
    if (!hasAttrs || !(allowedRule instanceof Set)) {
        return `&lt;${tagName}&gt;`;
    }

    const filterResult = [`&lt;${tagName}`];

    const parser = new HTMLParser.Parser(htmlParserHandler);
    parser.parseComplete(`${tag}&lt;/${tagName}&gt;`);
    const firstChild = htmlParserHandler.dom &amp;&amp; htmlParserHandler.dom[0];
    const attrs = firstChild &amp;&amp; firstChild.attribs;
    if (attrs) {
        Object.keys(attrs).forEach(attrName =&gt; {
            if (allowedRule.has(attrName)) {
                const attrValue = attrs[attrName];
                const quoteType = attrValue.includes(&apos;&quot;&apos;) ? &apos;\&apos;&apos; : &apos;&quot;&apos;;
                filterResult.push(`${attrName}=${quoteType}${attrValue}${quoteType}`);
            }
        });
    }
    filterResult.push(&apos;&gt;&apos;);
    return filterResult.join(&apos; &apos;);
};

/**
 * &#x521D;&#x59CB;&#x5316; Marked
 */
Marked.setOptions({
    renderer,
    breaks: false,     // If true, use GFM hard and soft line breaks. Requires gfm be true.
    gfm: true,         // If true, use approved GitHub Flavored Markdown (GFM) specification.
    sanitize: true,    // If true, sanitize the HTML passed into markdownString with the sanitizer function.
    sanitizer: Config.ui[&apos;chat.markdown.html&apos;] ? sanitizer : null, // A function to sanitize the HTML passed into markdownString.
    headerIds: false,
    smartLists: true,  // If true, use smarter list behavior than those found in markdown.pl.
    smartypants: true, // If true, use &quot;smart&quot; typographic punctuation for things like quotes and dashes.
});

/**
 * Marked &#x6A21;&#x5757;
 * @name Marked
 * @see https://github.com/markedjs/marked
 * @static
 */
export default Marked;
</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
